package com.edgardleal.pixelruler;

import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.JMenuItem;
import javax.swing.JPopupMenu;

public class Menu extends JPopupMenu implements ActionListener {
	private static final long serialVersionUID = -3265132704295682699L;
	private JMenuItem fechar = null;

	public Main getMain() {
		return main;
	}

	public void setMain(Main main) {
		this.main = main;
	}

	public void setFechar(JMenuItem fechar) {
		this.fechar = fechar;
	}

	public JMenuItem getFechar() {
		if (fechar == null) {
			fechar = new JMenuItem("Fechar");
			fechar.addActionListener(this);
		}
		return fechar;
	}

	Main main = null;

	public Menu(Main main) {
		this.add(getFechar());
		this.main = main;
	}

	public Menu(String label) {
		super(label);
	}

	@Override
	public void actionPerformed(ActionEvent e) {
		if (e.getSource() == getFechar()) {
			onFecharClick();
		}
	}

	private void onFecharClick() {
		main.saveToFile();
		main.dispose();
		main = null;
	}
}
